package com.seanzx.resolver.impl;

import com.seanzx.common.Result;
import com.seanzx.constant.SymbolTable;
import com.seanzx.lexical.Token;
import com.seanzx.resolver.IResolver;
import com.seanzx.dp.ProcessorController;
import com.seanzx.dp.entity.SetVariableEntity;

import java.util.List;

public class SetVariableResolver implements IResolver<SetVariableEntity> {
    ProcessorController processorController = ProcessorController.get();

    @Override
    public boolean matches(List<Token> tokens) {
        return tokens.size() == 4 &&
                SymbolTable.SET.equals(tokens.get(0).getValue()) &&
                "=".equals(tokens.get(2).getValue());
    }

    @Override
    public SetVariableEntity translate(List<Token> tokens) {
        SetVariableEntity setVariableEntity = new SetVariableEntity();
        setVariableEntity.setKey(tokens.get(1).getValue());
        setVariableEntity.setValue(tokens.get(3).getValue());
        return setVariableEntity;
    }

    @Override
    public boolean validate(Object parameter) {
        return true;
    }

    @Override
    public Result access(Object parameter) {
        SetVariableEntity entity = (SetVariableEntity) parameter;
        return processorController.setProperties(entity.getKey(), entity.getValue());
    }
}
